﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VirtualPress.Infrastructure.HitMonitor
{
    public abstract class BaseMonitor: IDisposable
    {
        public BaseMonitor(object key)
        {
            this.key = key;
        }
        protected object key;

        protected object OriginalValue { get; set; }
        protected object CurrentValue { get; set; }

        protected abstract void LoadAction();
        internal abstract void Flush();

        public abstract void Trigger(int action,int up=1);

        public T GetValue<T>(bool isTrigger=false, int getAction = -1)
            where T:class
        {
            if (CurrentValue == null)
            {
                LoadProperties();
            }
            if (isTrigger)
            {
                Trigger(getAction);
            }
            return CurrentValue as T;
        }

        protected void LoadProperties()
        {
            if (CurrentValue == null)
            {
                lock (this) 
                {
                    if (CurrentValue == null)
                    {
                        LoadAction();
                    }
                }
            }
        }

        public abstract void Dispose();
    }
}
